/*
 * 基本查询 查询索引、查询进度、停止查询
 * */

package com.axt.dbaudit.bean.query;

import com.axt.dbaudit.base.*;
import com.axt.dbaudit.model.query.QueryProcess;
import com.axt.soa.bean.*;
import com.axt.soa.msg.record.DBRecord;
import com.axt.soa.msg.types.*;

import java.util.*;

public class QueryBaseBean extends BaseFuncBean {
	protected int queryType = 0;
	
	/* 查询输入信息 */
	protected List<QueryFilter> filters = new ArrayList<QueryFilter>(); 
	protected int sort = 0;
	protected int queryInResult = 0;
	protected int statDim = 0;
	protected int maxRowNum = 0;
	protected HashMap<String, String> paramsIn = new HashMap<String, String>();
	
	/* 查询返回结果 */
	protected int totalRows = 0;
	protected List record = null;
	protected HashMap<String, String> paramsOut = new HashMap<String, String>();
	protected String queryProgress = "";
	protected String queryDate = "";
	
	protected int getQueryType() {
		return queryType;
	}
	
	protected void setQueryType(int queryType) {
		this.queryType = queryType;
	}

	protected void setFilters(List<QueryFilter> filters) {
		this.filters = filters;
	}
	
	protected void setSort(int sort) {
		this.sort = sort;
	}
	
	protected void setQueryInResult(int queryInResult) {
		this.queryInResult = queryInResult;
	}

	protected void setStatDim(int statDim) {
		this.statDim = statDim;
	}

	protected void setMaxRowNum(int maxRowNum) {
		this.maxRowNum = maxRowNum;
	}

	protected void setParamsIn(HashMap<String, String> paramsIn) {
		this.paramsIn = paramsIn;
	}
	
	public int getTotalRows() {
		return totalRows;
	}
	
	protected List getRecord() {
		return record;
	}

	protected HashMap<String, String> getParamsOut() {
		return paramsOut;
	}
	
	public String getQueryProgress() {
		if (null == paramsOut)
			return "";
		
		return paramsOut.get(MsgParam.MSG_PARA_QUERY_PROGRESS).toString();
	}
	
	public String getQueryDate() {
		if (null == paramsOut)
			return "";
		
		return paramsOut.get(MsgParam.MSG_PARA_QUERY_DATE).toString();
	}
	
	/* initiate on query */
	public void init() {
		queryType = 0;
		filters = null;
		sort = 0;
		queryInResult = 0;
		statDim = 0;
		maxRowNum = 0;
		paramsIn = null;
		totalRows = 0;
		record = null;
		paramsOut = null;
	}

	/* common query interface */
	protected int query(int funcNo, String serverPath, String className) {
		/* prepare */
		prepare(serverPath);
		/* add to request info */
		addToRequest();
		
		/* request from server */
		record = fetch(funcNo, className, MsgParam.PARA_RECORD, CONN_T_TCP);
		
		if (getErrcode() != 0)
			return -1;
		
		/* get query out from reply info */
		parseFromReply();
		
		return 0;
	}
	
	/* common query with no record */
	protected int query(int funcNo, String serverPath) {
		/* prepare */
		prepare(serverPath);
		/* add to request info */
		addToRequest();
		
		/* request from server */
		execute(funcNo, CONN_T_TCP);
		
		if (getErrcode() != 0)
			return -1;
		
		return 0;
	}
	
	/* add to request */
	protected void addToRequest() {
		/* set parameter in */
		request.addParam(MsgParam.CSP_F_QUERY_MAXROW, maxRowNum);
	    request.addParam(MsgParam.CSP_F_QUERY_RSLT, queryInResult);
	    request.addParam(MsgParam.CSP_F_QUERY_TYPE, queryType);
	    request.addParam(MsgParam.CSP_F_QUERY_SORT, sort);
	    request.addParam(MsgParam.CSP_F_QUERY_STATDIM, statDim);
	    
	    /* set record */
	    DBRecord recordIn = new DBRecord();
	    recordIn.addColumn(MsgParam.CSP_F_FILTER_TYPE);
	    recordIn.addColumn(MsgParam.CSP_F_FILTER_RELATION);
	    recordIn.addColumn(MsgParam.CSP_F_FILTER_LOGIC);
	    recordIn.addColumn(MsgParam.CSP_F_VALUE);
	    
	    for (int i = 0; i < filters.size(); i ++) {
	    	recordIn.addIntValue(filters.get(i).getFilterType());
	    	recordIn.addIntValue(filters.get(i).getFilterRelation());
	    	recordIn.addIntValue(filters.get(i).getFilterLogic());
	    	recordIn.addStringValue(filters.get(i).getFilterValue());
	    }
	    
	    request.addRecord(MsgParam.CSP_F_QUERY_FILTER, recordIn);
	    
	    /* set parameters */
//	    Iterator<String> it = paramsIn.keySet().iterator();
//		while (it.hasNext()) {
//			String param = it.next();
//			String value = paramsIn.get(param);
//			request.addParam(param, value);
//		}
	}
	
	/* parse from reply */
	protected void parseFromReply() {
		queryType = reply.getParam(MsgParam.CSP_F_QUERY_TYPE).toInteger();
		totalRows = reply.getParam(MsgParam.CSP_F_TOTAL_ROWS).toInteger();
		
		List<String> params = reply.getParams();
		for (int i = 0; i < params.size(); i ++)
		{
			String param = params.get(i);
			String valueString = reply.getParam(param).toString();
			
			if(this.paramsOut == null)
				this.paramsOut = new HashMap<String, String>();
			this.paramsOut.put(param, valueString);
		}
	}
}
